package cn.xiuxius.askbox.config;

import cn.xiuxius.askbox.interceptor.JwtTokenAdminInterceptor;
import cn.xiuxius.askbox.interceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {



    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtTokenAdminInterceptor).addPathPatterns("/console/**").excludePathPatterns("/console/console/login");
        registry.addInterceptor(jwtTokenUserInterceptor).addPathPatterns("/user/users/home");
    }

    @Bean
    public Docket consoleDocket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("AskBox接口文档")
                .version("1.0")
                .description("AskBox接口文档")
                .build();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .groupName("控制台端")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.xiuxius.askbox.controller.console"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket userDocket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("AskBox接口文档")
                .version("1.0")
                .description("AskBox接口文档")
                .build();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .groupName("用户接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.xiuxius.askbox.controller.user"))
                .paths(PathSelectors.any())
                .build();
    }
    @Bean
    public Docket commonDocket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("AskBox接口文档")
                .version("1.0")
                .description("AskBox接口文档")
                .build();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .groupName("公共接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.xiuxius.askbox.controller.common"))
                .paths(PathSelectors.any())
                .build();
    }

    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
